<?php
/**
 * Created by PhpStorm.
 * User: Thai Dac Vinh
 * Date: 9/30/14
 * Time: 11:43 PM
 */
if (isset($_COOKIE["{$session_key}-learned"]) && ($_COOKIE["{$session_key}-learned"] != 0)) {
    $key = $_COOKIE["{$session_key}-learned"];
    unset($_COOKIE["{$session_key}-learned"]);
    setcookie("{$session_key}-learned", '', time() - 3600); // empty value and old timestamp
    if (!isset($_SESSION[$session_key]['card_info'][$key])) {
        set_en_card_learned_count($key, 1);
        $_SESSION[$session_key]['card_info'][$key] = 1;
    } else {
        $new_key_value = $_SESSION[$session_key]['card_info'][$key] + 1;
        set_en_card_learned_count($key, $new_key_value);
        $_SESSION[$session_key]['card_info'][$key] = $new_key_value;
    }
    $card_info = $_SESSION[$session_key]['card_info'];
    $vocab->update_card_info($card_info);
    $_SESSION[$session_key]['vocab'] = serialize($vocab);
}

if ((isset($_COOKIE["{$session_key}-correct"]) && ($_COOKIE["{$session_key}-correct"] != 0))
    || (isset($_COOKIE["{$session_key}-incorrect"]) && ($_COOKIE["{$session_key}-incorrect"] != 0))) {
    $correct = 0;
    $incorrect = 0;
    if (isset($_COOKIE["{$session_key}-correct"]) && ($_COOKIE["{$session_key}-correct"] != 0)) {
        $correct = $_COOKIE["{$session_key}-correct"];
    }
    if (isset($_COOKIE["{$session_key}-incorrect"]) && ($_COOKIE["{$session_key}-incorrect"] != 0)) {
        $incorrect = $_COOKIE["{$session_key}-incorrect"];
    }
    $key_correct = "${year}-correct";
    $key_incorrect = "${year}-incorrect";

    unset($_COOKIE["{$session_key}-correct"]);
    unset($_COOKIE["{$session_key}-incorrect"]);
    setcookie("{$session_key}-correct", '', time() - 3600); // empty value and old timestamp
    setcookie("{$session_key}-incorrect", '', time() - 3600); // empty value and old timestamp
//    if (!isset($_SESSION[$session_key]['card_info'][$key_incorrect])) {
    $vocab->card_tested_correct_count = $vocab->card_tested_correct_count + $correct;
    set_en_card_tested_count($key_correct, $vocab->card_tested_correct_count);
    $_SESSION[$session_key]['card_info'][$key_correct] = $vocab->card_tested_correct_count;
    $vocab->card_tested_incorrect_count = $vocab->card_tested_incorrect_count + $incorrect;
    set_en_card_tested_count($key_incorrect, $vocab->card_tested_incorrect_count);
    $_SESSION[$session_key]['card_info'][$key_incorrect] = $vocab->card_tested_incorrect_count;
    $_SESSION[$session_key]['vocab'] = serialize($vocab);
    $card_info = $_SESSION[$session_key]['card_info'];
}

// Shuffle
if (!isset($_COOKIE[$session_key.'-shuffle'])) $_COOKIE[$session_key.'-shuffle'] = 1;
$shuffle = $_COOKIE[$session_key.'-shuffle'];
$shuffle_overturn = ($shuffle == 1 ? 0 : 1);